

import config from "../config/index.js";

export default{
  setSession(key,data) {
  
    // sessionStorage.setItem(config.namespace+'-'+name,res)
    let storage=this.getStorage()
    storage[key]=data
     sessionStorage.setItem(config.namespace,JSON.stringify(storage))
},
  getSession(key) {
    
    return this.getStorage()[key]
},

clearitem(key){
  let storage=this.getStorage()
  delete storage[key]
  sessionStorage.setItem(config.namespace,JSON.stringify(storage))
},
clearAll(){
  sessionStorage.clear()
},

 getStorage() {
    return JSON.parse(sessionStorage.getItem(config.namespace))||{}
},
 throttle(fn, wait = 800) {
    let that=this
    let previous = 0//记录时间
    return function proxy() {
        let now = +new Date()
        if (now - previous > wait) {
            fn.apply(that,arguments)
            previous=now
        }

    }
},
  debounce(handle,wait=300,search){
    console.log(search);
      // 参数判断
      let timer  = null
     console.log(handle);
      return function proxy(){ 
        //this 指向btn
        console.log('proxy',this)
        let that = this
        let arg = arguments //arguments不定参 当传递的参数不确定时 可以使用这个
        clearTimeout(timer)
        timer =  setTimeout(()=>{
          handle.call(that,arg[0])
          //hanlde没有执行
        },wait)
  
      }
    },
    formateDate(date, rule) {
      let fmt = rule || "yyyy-MM-dd hh:mm:ss";
      if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, date.getFullYear());
      }
      const o = {
        // 'y+': date.getFullYear(),
        "M+": date.getMonth() + 1,
        "d+": date.getDate(),
        "h+": date.getHours(),
        "m+": date.getMinutes(),
        "s+": date.getSeconds(),
      };
      for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
          const val = o[k] + "";
          fmt = fmt.replace(
            RegExp.$1,
            RegExp.$1.length == 1 ? val : ("00" + val).substr(val.length)
          );
        }
      }
      return fmt;
    },
}


